package hos.ui.bean;


import android.text.TextUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>Title: CheckMore </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @version : 1.0
 * @date : 2023/5/17 15:09
 */
public class CheckMore {

    private String id = "CheckMore";

    private List<String> titles = new ArrayList<>();
    private List<String> codes = new ArrayList<>();
    private List<Boolean> bools = new ArrayList<>();
    private List<String> checks = new ArrayList<>();

    private List<Object> tags = new ArrayList<>();

    public CheckMore(String id) {
        this.id = id;
    }

    public CheckMore() {
        this("CheckSingle");
    }

    public CheckMore(List<String> titles, List<String> codes) {
        this();
        this.titles = titles;
        this.codes = codes;
    }

    public CheckMore(String id, List<String> titles, List<String> codes) {
        this.id = id;
        this.titles = titles;
        this.codes = codes;
    }

    public List<Boolean> getBools() {
        return bools;
    }

    public boolean[] getBoolArray() {
        boolean[] booleans = new boolean[bools.size()];
        for (int i = 0; i < bools.size(); i++) {
            booleans[i] = bools.get(i);
        }
        return booleans;
    }

    public List<String> getTitles() {
        return titles;
    }

    public String[] getTitleArray() {
        return titles.toArray(new String[]{});
    }

    public List<String> getCodes() {
        return codes;
    }

    public List<Object> getTags() {
        return tags;
    }

    public void setTags(List<Object> tags) {
        this.tags = tags;
    }

    public List<String> getChecks() {
        return checks;
    }

    public String getId() {
        return id;
    }

    public String title(int index) {
        if (index < 0 || titles.isEmpty() || titles.size() <= index) {
            return null;
        }
        return titles.get(index);
    }

    public String code(int index) {
        if (index < 0 || codes.isEmpty() || codes.size() <= index) {
            return null;
        }
        return codes.get(index);
    }

    public String check(int index, boolean isChecked) {
        bools.set(index, isChecked);
        if (!checks.contains(codes.get(index))) {
            checks.add(codes.get(index));
        }
        return code(index);
    }

    public String getCheckTitle() {
        return getCheckTitle(",");
    }

    public String getCheckTitle(String split) {
        StringBuilder nameBuilder = new StringBuilder();
        for (int i = 0; i < bools.size(); i++) {
            if (!bools.get(i)) {
                continue;
            }
            if (nameBuilder.length() != 0) {
                nameBuilder.append(split);
            }
            nameBuilder.append(titles.get(i));
        }
        return nameBuilder.toString();
    }

    public String[] getCheckSemicolon() {
        return getCheck(";");
    }

    public String[] getCheck() {
        return getCheck(",");
    }

    public String[] getCheck(String split) {
        checks.clear();
        StringBuilder nameBuilder = new StringBuilder();
        StringBuilder codeBuilder = new StringBuilder();
        for (int i = 0; i < bools.size(); i++) {
            if (!bools.get(i)) {
                continue;
            }
            checks.add(codes.get(i));
            if (nameBuilder.length() != 0) {
                nameBuilder.append(split);
            }
            nameBuilder.append(titles.get(i));
            if (codeBuilder.length() != 0) {
                codeBuilder.append(split);
            }
            codeBuilder.append(codes.get(i));
        }
        return new String[]{nameBuilder.toString(), codeBuilder.toString()};
    }

    /**
     * 填充数据
     * @param data      数据
     */
    public String[] checkData(String split, String data) {
        if (TextUtils.isEmpty(data)) {
            return new String[]{getTitles().get(0), getCodes().get(0)};
        }
        StringBuilder name = new StringBuilder();
        List<String> codeList = getCodes();
        for (int i = 0; i < codeList.size(); i++) {
            String code = codeList.get(i);
            if (!data.contains(split)) {
                // 说明只有一条数据
                if (TextUtils.equals(data, code)) {
                    name.append(getTitles().get(i));
                    check(i, true);
                    break;
                }
            } else {
                // 有多个数据
                String[] dataList = data.split(split);
                for (String dataSplit : dataList) {
                    if (TextUtils.equals(dataSplit, code)) {
                        if (name.length() != 0) {
                            name.append(split);
                        }
                        name.append(getTitles().get(i));
                        check(i, true);
                        break;
                    }
                }
            }
        }
        return new String[]{name.toString(), data};
    }

    public void clear() {
        titles.clear();
        codes.clear();
    }

    public interface OnCheckCallListener {
        void onCheck(String title, String code);
    }
}
